/**
 * 治理相关 API
 */

import request from '../request'

/**
 * 提案状态枚举
 */
export enum ProposalStatus {
  PENDING = 'PENDING',
  ACTIVE = 'ACTIVE',
  PASSED = 'PASSED',
  REJECTED = 'REJECTED',
  EXECUTED = 'EXECUTED',
  CANCELLED = 'CANCELLED',
}

/**
 * 提案信息接口
 */
export interface ProposalInfo {
  id: string
  familyId: string
  title: string
  description: string
  proposer: string
  status: ProposalStatus
  votesFor: number
  votesAgainst: number
  totalVotes: number
  startTime: string
  endTime: string
  executedAt?: string
  createdAt: string
}

/**
 * 投票记录接口
 */
export interface VoteRecord {
  proposalId: string
  voter: string
  support: boolean
  weight: number
  votedAt: string
}

/**
 * 创建提案请求参数
 */
export interface CreateProposalRequest {
  familyId: string
  title: string
  description: string
  duration?: number // 投票持续时间（秒）
}

/**
 * 投票请求参数
 */
export interface VoteRequest {
  proposalId: string
  support: boolean
  reason?: string
}

/**
 * 治理 API
 */
export const governanceApi = {
  /**
   * 创建提案
   */
  createProposal: (data: CreateProposalRequest) =>
    request.post<{ proposalId: string; txHash: string }>('/blockchain/governance/proposal', data),

  /**
   * 投票
   */
  vote: (data: VoteRequest) =>
    request.post<{ txHash: string }>('/blockchain/governance/vote', data),

  /**
   * 获取提案详情
   */
  getProposal: (proposalId: string) =>
    request.get<ProposalInfo>(`/blockchain/governance/proposal/${proposalId}`),

  /**
   * 获取家族的提案列表
   */
  getProposals: (familyId: string) =>
    request.get<ProposalInfo[]>(`/blockchain/governance/proposals/${familyId}`),

  /**
   * 获取提案的投票记录
   */
  getVotes: (proposalId: string) =>
    request.get<VoteRecord[]>(`/blockchain/governance/proposal/${proposalId}/votes`),

  /**
   * 执行提案
   */
  executeProposal: (proposalId: string) =>
    request.post<{ txHash: string }>(`/blockchain/governance/proposal/${proposalId}/execute`),

  /**
   * 取消提案
   */
  cancelProposal: (proposalId: string) =>
    request.post<{ txHash: string }>(`/blockchain/governance/proposal/${proposalId}/cancel`),

  /**
   * 获取用户的投票记录
   */
  getUserVotes: (address: string) =>
    request.get<VoteRecord[]>(`/blockchain/governance/user/${address}/votes`),

  /**
   * 检查用户是否已投票
   */
  hasVoted: (proposalId: string, address: string) =>
    request.get<{ hasVoted: boolean }>(`/blockchain/governance/proposal/${proposalId}/has-voted/${address}`),

  /**
   * 获取提案统计
   */
  getProposalStats: (familyId: string) =>
    request.get<{
      total: number
      active: number
      passed: number
      rejected: number
    }>(`/blockchain/governance/stats/${familyId}`),
}

export default governanceApi

